<?php
class ControlPlugin extends Plugin
{
    private $auth;

    public function startup()
    {
        parent::startup();
        $this->auth = new Auth;
    }

    public function onEvent($type, $data)
    {

    }

    public function getMessage(Message $data)
    {
        if(!$data->isInRoom()){
            if(($data->getBody() == '!shutdown' OR
                $data->getBody() == '!restart'
                ) AND $data->body[0] == '!'){
                
                $command = substr($data->getBody(), 1);

                if(!$this->auth->isAllowAction($this->auth->getJidFromLastNick($data->getFrom()),$command) OR (
                        !empty($data->jid) AND
                        !$this->auth->isAllowAction($data->getJid(),$command))){
                    $this->sendMessage('You do not have access: '.$command, $data->getFrom());
                    return;
                }
                
                if($command == 'shutdown'){
                    $this->sendMessage('minBot shutdown', $data->getFrom());
                    $this->getBase()->shutdown();

                }else{
                    $this->sendMessage('Restart !', $data->getFrom());
                    $this->getBase()->restart();
                }
                
            }elseif($data->getBody() == '!test'){
                $this->sendMessage('Hi '.$data->getFrom().'! Jid: '.$data->getJid(), $data->getFrom());

            }
        }
        
        if(strpos( $data->getBody(),'!say ') === 0){
              $this->sendMessage(substr($data->getBody(), strlen('!say ')  ),$data->isInRoom()?NULL:$data->getFrom());

        }elseif($data->getBody() == '!about'){
              $this->sendMessage("Hi, I'm minBot \n my home is http://minbot.sodae.cz.",$data->isInRoom()?NULL:$data->getFrom());
        }
    }
}
?>